package com.easychat.utils;

import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 10 2024/10/19
 */
@Component
public class RedisUtil {
    @Resource
    private RedisTemplate redisTemplate;

    public void lpushAll(String key,List<String> values,Integer expireTime){
        for (String value : values){
            redisTemplate.opsForList().leftPush(key,value);
            redisTemplate.expire(key,expireTime, TimeUnit.SECONDS);
        }
    }
    public void lpushAll(String key,String value,Integer expireTime){
            redisTemplate.opsForList().leftPush(key,value);
            redisTemplate.expire(key,expireTime, TimeUnit.SECONDS);

    }
    public Object rpopAll(String key){
       return redisTemplate.opsForList().range(key,0,-1);
    }

    public void remove(String userId, String contactId) {
        redisTemplate.afterPropertiesSet();
        redisTemplate.boundListOps(userId).remove(0,contactId);
    }
}
